<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Facebook_m extends CI_Model{
	
	function insert_batch($data){
		$tmp = array();
		foreach($data as $obj)
			array_push($tmp, $obj['code']);
		$code = implode("','", $tmp);
		$code = "'".$code."'";
		$sql = "DELETE FROM seo_accounts WHERE code IN ($code)";
		$this->db->query($sql, false);
		$this->db->insert_batch('seo_accounts', $data);
		return true;
	}
	
	function get_all_active($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	12,
			'order_by'	=>	'created',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'active'		=>	-1,
			'name'		=> false,
		);
		$def = array_merge($def, $data);
		$this->db->where('active', 1);
		if($def['name'])
			$this->db->where("name LIKE '%{$def['name']}%'", false, false);
		if($is_count)
			return count($this->db->get('seo_accounts')->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('name', 'ASC');
		return 
		$this->db->get('seo_accounts')->result();
	}
	
	
	function get_files_queue($data = array(), $is_count = false){
		$def = array(
			'limit'		=>	10,
			'order_by'	=>	'created',
			'order'		=>	'DESC',
			'paged'		=>	0,
			'fb_nick'	=> false,
			'status'		=> false
		);
		$def = array_merge($def, $data);
		if($def['fb_nick'])
			$this->db->where("fb_nick LIKE '%{$def['fb_nick']}%'", false, false);
		if($is_count)
			return count($this->db->get('seo_queue')->result());
		if($def['limit'])
			$this->db->limit($def['limit'], $def['paged']);
		if($def['order_by']&&$def['order'])
			$this->db->order_by($def['order_by'], $def['order']);		
		$this->db->order_by('fb_nick', 'ASC');
		return 
		$this->db->get('seo_queue')->result();
	}
	
	function save_queue($data){
		if(isset($data['id'])){
			$this->db->where('id', $data['id']);
			unset($data['id']);
			return $this->db->update('seo_queue', $data);
		}else{
			return $this->db->insert('seo_queue', $data);
		}
	}
}